﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
namespace DataApi
{
    public class EpicCmsResult : ActionResult
    {
        /// <summary>
        /// A single parameter, of type string, function that returns a db collection result
        /// </summary>
        /// <param name="param1">The first param</param>
        /// <returns></returns>
        public delegate List<Dictionary<String, String>> DbExecuteWithNoParam();
        public delegate List<Dictionary<String, String>> DbExecuteWithOneParam(Object param1);
        public delegate List<Dictionary<String, String>> DbExecuteWithTwoParam(Object param1, Object param2);
        public DbExecuteWithNoParam ExecuteWithNoParam = null;
        public DbExecuteWithOneParam ExecuteWithOneParam = null;
        public DbExecuteWithTwoParam ExecuteWithTwoParam = null;
        public Object Param1 = null;
        public Object Param2 = null;

        public override void ExecuteResult(ControllerContext context)
        {
            // Attempt to run the method
            try
            {
                List<Dictionary<String, String>> results = null;

                // If the delegate for executing a function with one parameter is not null
                if (ExecuteWithNoParam != null)
                    // Get the results from the function
                    results = ExecuteWithNoParam();

                if (ExecuteWithOneParam != null)
                    // Get the results from the function
                    results = ExecuteWithOneParam(Param1);
                // If the delegate for executing a function with two parameters is not null
                else if (ExecuteWithTwoParam != null)
                    // Get the results from the function
                    results = ExecuteWithTwoParam(Param1, Param2);

                // If there are results
                if (results != null && results.Count > 0)
                {
                    // Return the resultset as JSON
                    /// TODO: add XML return type, CSV return type, Xlsx return type
                    ReturnJson(results);
                    return;
                }
            }
            // If there was an error running the method
            catch (Exception e)
            {
                // return the error
                ReturnError(e);
                return;
            }

            // Return 'no data' as json string
            ReturnJson("no data");
            return;
        }

        /// <summary>
        ///  Writes json to the response
        /// </summary>
        /// <param name="json">The json string to write to the response</param>
        public void ReturnJson(String json)
        {
            HttpContext.Current.Response.AddHeader("content-type", "application/json");
            HttpContext.Current.Response.Write(json);
        }

        /// <summary>
        /// Converts the object to json and writes it to the response
        /// </summary>
        /// <param name="jsonObj">The object to convert and write</param>
        public void ReturnJson(object jsonObj)
        {
            // Convert the object to a json string
            String json = JsonConvert.SerializeObject(jsonObj);
            ReturnJson(json);
        }

        /// <summary>
        /// Write an error to the response
        /// </summary>
        /// <param name="e">The exception to get error information from</param>
        public void ReturnError(Exception e)
        {
            // Create the error return object
            Dictionary<string, string> error = new Dictionary<string, string>();
            // Add the error message
            error.Add("error", e != null ? e.Message : "An error occured");
            // Return the error
            ReturnJson(error);
        }
    }
}